package com.windgo.dao;

import com.windgo.bean.MarketAdmin;
import com.windgo.bean.MarketLog;
import com.windgo.bean.MarketUser;
import com.windgo.service.AdminLogService;
import com.windgo.service.AdminService;
import com.windgo.service.UserService;
import com.windgo.util.RequestUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author 张林浩
 * @since 2022/10/15 10:51
 */

@Component
@Aspect
public class CustomerAspectJ {

    @Autowired
    AdminLogService adminLogService;

    @Autowired
    AdminService adminService;

    @Autowired
    UserService userService;



    @Pointcut("execution(* com.windgo.controller.admin..*(..)) || execution(* com.windgo.controller.wx..*(..)) ")
    public void Pointcut() {
    }


    @Around("Pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        Object proceed = joinPoint.proceed();


        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            HttpServletRequest request = RequestUtils.getHttpServletRequest();
            String uri = RequestUtils.getUri(request);
            String ip = RequestUtils.getIp(request);
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1 = simpleDateFormat.format(date);
            if (uri.startsWith("/admin")){
                MarketAdmin marketAdmin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
                choseUri(uri, ip, date, date1, subject, marketAdmin);
            }else if (uri.startsWith("/wx")){
                MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
                choseUri1(uri, ip, date, date1, subject, marketUser);
            }




        }
        return proceed;
    }

    private void choseUri(String uri, String ip, Date date, String date1, Subject subject, MarketAdmin marketAdmin) {
        if ("/admin/auth/login".equals(uri)) {
            if (subject.isAuthenticated()) {
                adminLogService.printLogOfAdminLogin(new MarketLog(null, marketAdmin.getUsername(), ip, 1, "登录", true, null, null, date1, null, false));
                adminService.updateIpAndLastLoginTime(ip, date, marketAdmin.getId());
            } else {
                adminLogService.printLogOfAdminLogin(new MarketLog(null, marketAdmin.getUsername(), ip, 1, "登录", false, "认证失败", null, date1, null, false));
            }
        } else if ("/admin/auth/logout".equals(uri)) {
            if (subject.isAuthenticated()) {
                adminLogService.printLogOfAdminLogin(new MarketLog(null, marketAdmin.getUsername(), ip, 1, "登出", true, null, null, date1, null, false));
             }
        }else if ("/admin/admin/update".equals(uri)){
            adminLogService.printLogOfAdminLogin(new MarketLog(null,marketAdmin.getUsername(),ip,1,"编辑管理员",true,null,null,date1,null,false));
        }else if ("/admin/admin/create".equals(uri)){
            adminLogService.printLogOfAdminLogin(new MarketLog(null,marketAdmin.getUsername(),ip,1,"添加管理员",true,null,null,date1,null,false));
        }else if ("/admin/order/ship".equals(uri)){
            adminLogService.printLogOfAdminLogin(new MarketLog(null,marketAdmin.getUsername(),ip,2,"发货",true,null,null,date1,null,false));
        }else if ("/admin/order/refund".equals(uri)){
            adminLogService.printLogOfAdminLogin(new MarketLog(null,marketAdmin.getUsername(),ip,2,"退款",true,null,null,date1,null,false));
        }
    }
    private void choseUri1(String uri, String ip, Date date, String date1, Subject subject, MarketUser marketUser) {
        if (("/wx/auth/login".equals(uri))){
            MarketUser marketUser1 = new MarketUser();
            marketUser1.setLastLoginIp(ip);
            marketUser1.setLastLoginTime(date1);
            marketUser1.setId(marketUser.getId());
            userService.updateLoginTimeAndIp(marketUser1);
        }
    }

}
